﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;


using OakEngine.AI.State_Machine;
using OakEngine.AI.Tools;
using OakEngine.BaseObject;

namespace OakEngine.AI.Behavior_State
{
   public class Oak_SeekBehaviorState : Oak_IState<OAK_BaseGameEntity>
   {
       private Vector2 m_vecPosition;
       private const float m_fHysteresis = 15.0f;
       private float m_fSeekMaxDistance;
       private float m_fSeekDistanceFromTarget;
       private float m_fTargetDistance;
       private float m_fWanderMaxDistance;


       public void Enter(OAK_BaseGameEntity type)
       {
           m_fSeekMaxDistance = 250.0f;
           m_fWanderMaxDistance = 60.0f;

       }

       public void Execute(OAK_BaseGameEntity type)
       {
           // Update every info about this, and the target we want to seek
           m_vecPosition = type.Position;

           // Hold a curreent distance and add a padding for jitter issues
           float SeekSafeDistance = m_fSeekMaxDistance;
           float WanderSafeDistance = m_fWanderMaxDistance;

           SeekSafeDistance += m_fHysteresis * 0.5f;
           WanderSafeDistance -= m_fHysteresis * 0.5f;

           // Getting distance from target to me
           m_fSeekDistanceFromTarget = Vector2.Distance(type.Position, type.TargetPosition);

           if (m_fSeekDistanceFromTarget > SeekSafeDistance)
           {
               // We would want to return back and go wandering or other behavior
               //type.ChangeState(new Oak_WanderBehaviorState());
           }
           
           // well looks like we can keep chasing
           // continue the loop, we can add another if check to see if we want to stop and rest

           // Oky we are going to keep seeking the player or what ever lets updare our orientation and Heading
           type.Rotation = OAK_AITools.TurnToAim(m_vecPosition, type.TargetPosition, type.Rotation, type.RotationRate);

           Vector2 heading = new Vector2((float)Math.Cos(type.Rotation), (float)Math.Sin(type.Rotation));
           //Vector2 heading = OAK_AITools.Vector2DRotate(type.Position, type.Rotation);

           type.Velocity = heading;

       }

       public void Exit(OAK_BaseGameEntity type)
       {
           // Nothing much to do here
       }


   }
}
